home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / (Demos) / FlickeringGrating.c < prev    next >
C/C++ Source or Header  |  1996-05-29  |  8KB  |  237 lines

  1. /*
  2. FlickeringGrating.c
  3. This demo displays a research-grade visual stimulus. If you just want to know
  4. how to load the clut and display a pattern, then you should start by reading
  5. Grating.c, which is much shorter and simpler.
  6. Copyright (c) 1989-1993 Denis G. Pelli
  7. HISTORY:
  8. 11/89     Lan & Denis wrote it.
  9. 23.1.90    dgp        Use second screen only if available.
  10. 4/9/90    dgp        Changed WindowPtr to CWindowPtr. Made big arrays static. Reduced
  11.                 default memory allocation to 1 megabyte. Use any 8-bit screen,
  12.                 preferably not the main screen.
  13. 4/23/90    dgp        Added optional timing. Centered the image. Left clut entries 0 and 
  14.                 clutSize-1 alone, so background doesn't flash. Asked if ISR Video 
  15.                 Attenuator is present.
  16. 10/11/90 dgp    Added fpu test.
  17. 10/29/90 dgp    Added CenterRectInRect().
  18. 10/30/90 dgp    Changed call to SetLuminances() to SetLuminancesAndRange() so that
  19.                 the range is now kept fixed throughout all the frames, to avoid
  20.                 flashes.
  21. 8/24/91    dgp        Made compatible with THINK C 5.0.
  22.                 If possible, use ReadLuminanceRecord().
  23. 4/26/92    dgp        RestoreCluts().
  24. 8/27/92    dgp        replace SysEnvirons() by Gestalt()
  25. 12/30/92 dgp    made more like Filter, using a small console so that, if necessary,
  26.                 both the console and the grating will fit on the main monitor.
  27. 2/7/93    dgp        replaced SetOnePixel by SetPixelsQuickly
  28. 7/7/93    dgp        added code for compatibility with Radius PowerView, a SCSI video box.
  29. 9/5/94 dgp removed assumption in printf's that int==short.
  30. 11/17/94 dgp Josh Solomon reported a stack overflow, so I added a calls to StackGrow
  31. and assert.
  32. 1/11/95    dgp    Tidied up various things. Now switch to color mode if using an ISR Attenuator.
  33. 4/11/95 dgp Save and restore the display depth and color mode.
  34. 6/1/95 dgp added code to support profiling in CodeWarrior.
  35. 6/14/95 dgp added call to ShieldCursor().
  36. */
  37. #include "VideoToolbox.h"
  38. #include "Luminance.h"
  39. #if (THINK_C || THINK_CPLUS || SYMANTEC_C)
  40.     #define SYMANTEC_C_PROFILE        0            // optionally, report timing
  41.     #include <profile.h>
  42. #endif
  43. #if __MWERKS__
  44.     #include <profiler.h>
  45. #endif
  46. #if UNIVERSAL_HEADERS
  47.     #include <LowMem.h>
  48. #else
  49.     #define LMGetMBarHeight() (* (short *) 0x0BAA)
  50.     #define LMSetMBarHeight(MBarHeightValue) ((* (short *) 0x0BAA) = (MBarHeightValue))
  51. #endif
  52.  
  53. #define SIZE         400            // size of grating, in pixels
  54. #define TMAX        6*67        // duration, in frames, typically at 67 Hz
  55. #define REPETITIONS    1            // Number of times to repeat the animation
  56.  
  57. void main(void);
  58. void FlickeringGrating(void);
  59. typedef struct{
  60.     ColorSpec table[256];
  61. }ColorSpecTable;
  62.  
  63. void main(void)
  64. {
  65.     StackGrow(40000L+2*SIZE*sizeof(double)+SIZE*sizeof(long)+sizeof(LuminanceRecord)+TMAX*sizeof(Ptr));
  66.     MaxApplZone();
  67.     Require(gestalt8BitQD);
  68.     FlickeringGrating();
  69. }
  70.     
  71. void FlickeringGrating(void)
  72. {
  73.     register int i;
  74.     int j,tmax,clutSize,error,oldPixelSize,oldIsColor;
  75.     double fX[SIZE],fY[SIZE];
  76.     double LMid,LMin,LMax,dL,a,contrast;
  77.     CWindowPtr window=NULL;
  78.     WindowPtr oldPort=NULL;
  79.     GDHandle device=NULL;
  80.     LuminanceRecord LR,*LP;
  81.     ColorSpecTable *tables[TMAX];    // an array of pointers to ColorSpec tables
  82.     char string[100];
  83.     Rect r;
  84.     Boolean attenuatorInstalled;
  85.     long finalTicks;
  86.     Point pt;
  87.     
  88.     assert(StackSpace()>8000);
  89.     #if SYMANTEC_C_PROFILE
  90.         InitProfile(200,3);    /* only needed if you want timing info */
  91.         _profile=0;
  92.     #endif
  93.     #if __MWERKS__ && __profile__
  94.         ProfilerInit(collectDetailed,bestTimeBase,110,20);
  95.         ProfilerSetStatus(0);
  96.     #endif
  97.     /* INITIALIZE QuickDraw */
  98.     MaximizeConsoleHeight();
  99.     #if (THINK_C || THINK_CPLUS || SYMANTEC_C)
  100.         console_options.title="\pFlickeringGrating";
  101.         console_options.nrows = 5;
  102.     #elif __MWERKS__
  103.         SIOUXSettings.rows=5;
  104.         SIOUXSettings.autocloseonquit=0;
  105.         SIOUXSettings.showstatusline=0;
  106.         SIOUXSettings.asktosaveonclose=0;
  107.     #else
  108.         InitGraf(&qd.thePort);
  109.         InitFonts();
  110.         InitWindows();
  111.         InitCursor();
  112.     #endif
  113.     printf("\n");    // make sure that oldPort is the console
  114.     GetPort(&oldPort);
  115.     printf("Welcome to FlickeringGrating.\n");
  116.  
  117.     for(i=8;i>=0;i--){
  118.         // look for a screen with 8-bit pixels.
  119.         device=GetScreenDevice(i);
  120.         if(device == NULL)continue;
  121.         if((*(*device)->gdPMap)->pixelSize==8)break;
  122.     }
  123.     do{
  124.         if(GetScreenDevice(1)!=NULL)i=ChooseScreen(i,"Which screen?");
  125.         else i=0;
  126.         device=GetScreenDevice(i);
  127.     }while(device==NULL);
  128.     oldIsColor=TestDeviceAttribute(device,gdDevType);
  129.     oldPixelSize=(*(*device)->gdPMap)->pixelSize;
  130.     if(NewPaletteManager() && (oldPixelSize!=8 || !oldIsColor))
  131.         SetDepth(device,8,1<<gdDevType,1);
  132.     if(device==NULL || (*(*device)->gdPMap)->pixelSize!=8)
  133.         PrintfExit("Sorry, I require 8 bits/pixel.\n");
  134.     sprintf(string,"LuminanceRecord%d.h",i);
  135.     LP=&LR;
  136.     i=ReadLuminanceRecord(string,LP,0);    /* try to read correct file */
  137.     if(i<=0){
  138.         #include "LuminanceRecord1.h"
  139.     }
  140.     attenuatorInstalled=Choose(0,"Have you installed an ISR Video Attenuator?\n",noYes,2);
  141.     printf("Using luminance calibration for screen %d calibrated %s by %s.\n"
  142.         ,(int)LP->screen,LP->date,LP->notes);
  143.     if(!attenuatorInstalled){
  144.         LP->r=0.0;
  145.         LP->g=1.0;
  146.         LP->b=0.0;
  147.     }
  148.     window=GDOpenWindow(device);
  149.     #if SYMANTEC_C_PROFILE
  150.         _profile=1;
  151.     #endif
  152.     #if __MWERKS__ && __profile__
  153.         ProfilerSetStatus(1);
  154.     #endif
  155.  
  156.     /* load clut with linear gray scale */
  157.     // We'll leave clut entries 0 (white) and clutSize-1 (black) alone,
  158.     // since they are used heavily by Apple's stuff. Window frames
  159.     // will mostly look normal if we leave those two entries alone.
  160.     clutSize=GDClutSize(device);
  161.     SetLuminances(device,LP,1,clutSize-2,LP->LMin,LP->LMax);
  162.         
  163.     /* Display a sinusoid with a gaussian envelope */
  164.     // Compute the image.
  165.     SetPort((WindowPtr)window);
  166.     PmBackColor(1+(long)(0.5+(clutSize-3)*0.5));
  167.     EraseRect(&window->portRect);
  168.     SetRect(&r,0,0,SIZE,SIZE);
  169.     CenterRectInRect(&r,&window->portRect);
  170.     for(i=0;i<SIZE;i++){
  171.         a=(i-SIZE/2)/(SIZE/6.);
  172.         fY[i]=exp(-a*a);
  173.         fX[i]=fY[i]*sin((i-SIZE/2)*(2.0*PI/80.0));
  174.     }
  175.     assert(StackSpace()>4L*SIZE+4000);
  176.     pt.h=pt.v=0;
  177.     LocalToGlobal(&pt);
  178.     ShieldCursor(&r,pt);
  179.     for(j=0;j<SIZE;j++){
  180.         unsigned long row[SIZE];
  181.         for(i=0;i<SIZE;i++) row[i]=1+(long)(0.5+(clutSize-3)*0.5*(1.0+fY[j]*fX[i]));
  182.         SetPixelsQuickly(r.left,j+r.top,row,SIZE);
  183.     }
  184.     ShowCursor();
  185.     // Allocate ColorSpec tables, one for each frame
  186.     for(i=0;i<TMAX;i++){
  187.         tables[i]=(ColorSpecTable *)NewPtr(sizeof(ColorSpecTable));
  188.         if(tables[i]==NULL){
  189.             printf("Only room for %d lookup tables (one per frame) ... continuing.\n",i);
  190.             break;
  191.         }
  192.     }
  193.     tmax=i;
  194.     // Compute lookup tables.
  195.     LMid=(LP->LMax+LP->LMin)/2.0;
  196.     contrast=(LP->LMax-LP->LMin)/(LP->LMax+LP->LMin);
  197.     LMax=LMid*(1.0+contrast);
  198.     LMin=LMid*(1.0-contrast);
  199.     SetLuminancesAndRange(device,LP,1,clutSize-2,LMid,LMid,LMin,LMax);    // blank screen
  200.     for(i=0;i<tmax;i++){
  201.         a=6.0*(i-tmax/2)/tmax;
  202.         dL=LMid*contrast*exp(-a*a)*sin((i-tmax/2)*(2.0*PI*3.0*0.015));
  203.         SetLuminancesAndRange(NULL,LP,1,clutSize-2,LMid-dL,LMid+dL,LMin,LMax);
  204.         *tables[i]= *(ColorSpecTable *)LP->table;
  205.     }
  206.     printf("Now displaying the animation ...\n");
  207.     Delay(60,&finalTicks);
  208.     for(j=0;j<REPETITIONS;j++){
  209.         for(i=0;i<tmax;i++){
  210.             // This is a thinly disguished call to GDSetEntries.
  211.             LoadLuminances(device,(LuminanceRecord *) tables[i],1,clutSize-2);
  212.         }
  213.     }
  214.     #if SYMANTEC_C_PROFILE
  215.         _profile=0;
  216.     #endif
  217.     #if __MWERKS__ && __profile__
  218.         ProfilerSetStatus(0);
  219.     #endif
  220.     for(i=0;i<tmax;i++)DisposPtr((Ptr)tables[i]);
  221.     SetPort(oldPort);
  222.     GDDisposeWindow(window);
  223.     if(NewPaletteManager())
  224.         error=SetDepth(device,oldPixelSize,1<<gdDevType,oldIsColor);    // restore
  225.     else RestoreCluts();
  226.     #if (THINK_C || THINK_CPLUS || SYMANTEC_C) && !SYMANTEC_C_PROFILE
  227.         if(tmax==TMAX)abort();
  228.     #endif
  229.     #if __MWERKS__ && __profile__
  230.         ProfilerDump("\pFlickeringGrating.Profile");
  231.         ProfilerTerm();
  232.     #endif
  233.     #if __MWERKS__
  234.         if(tmax==TMAX)SIOUXSettings.autocloseonquit=1;
  235.     #endif
  236. }
  237.